package com.detroitchow;

import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;


public class SearchActivity extends Activity  implements OnItemClickListener {

	private StoreService storeService= null;
	private AutoCompleteTextView textView;
	private ArrayList<String> autoCompleteArray=null;
	private ListView storesListView=null;
	private ArrayAdapter<StoreVO> storesArrayAdapter=null;
	private ArrayList<StoreVO> stores=null;
	private Intent myIntent;
	
	@Override
  protected void onCreate(Bundle savedInstanceState) {
	  // TODO Auto-generated method stub
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.search);
	  textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
	  storesListView = (ListView) this.findViewById(R.id.lvStores);
	  storeService = ((StoreService)getApplicationContext());
	  if(storeService.isStoresAlive()) {
	  	
	  	autoCompleteArray = storeService.getAutoCompleteArray();
		  LogUtil.log("Search onCreate", "" + autoCompleteArray.size());
		  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.search_item, autoCompleteArray);
	    textView.setAdapter(adapter);
	    textView.setOnKeyListener(keyListener);
	    //textView.setOnItemSelectedListener(selectedItemListener);
	    textView.setOnItemClickListener(itemClickListener);
	    //textView.setO
	    
	  } else {
	  	myIntent = new Intent(this, Main.class);
 		 startActivityForResult(myIntent, 0);
	  }
	  
   
    
	  
	  
  }

	public SearchActivity() {
		// TODO Auto-generated constructor stub
	}
	
	private void executeSearch(String s) {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);
		storesListView.removeAllViewsInLayout();
		stores = storeService.getSearch(s);
		storesArrayAdapter = new StoresArrayAdapter(this, R.layout.directoryitem, R.id.txtstoretitle, stores);
    if(storesArrayAdapter != null){
    	storesListView.setAdapter(storesArrayAdapter);
      storesListView.setTextFilterEnabled(true);
      storesListView.setOnItemClickListener(this);
      
    }
	}

	
	private OnKeyListener keyListener = new OnKeyListener() {
		
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if (event.getAction() == KeyEvent.ACTION_DOWN)
	    {
	        switch (keyCode)
	        {
	            case KeyEvent.KEYCODE_DPAD_CENTER:
	            case KeyEvent.KEYCODE_ENTER:
	               //LogUtil.log("keyListener", "Picked Up enter Key search text: " + textView.getEditableText());
	            	
	               executeSearch(textView.getEditableText().toString());
	            default:
	                break;
	        }
	    }
	    return false;
	  }

		
	};
	
	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
	    // TODO Auto-generated method stub
			String selection =  (String) arg0.getItemAtPosition(position);
			LogUtil.log("Search", "onItemClicked position: " + position + " selectedObject: " + selection);
			textView.setText(selection);
			executeSearch(selection);
    }
		
	};
	
	private OnItemSelectedListener selectedItemListener = new OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> arg0, View view, int position,long arg3) {
	    // TODO Auto-generated method stub
			LogUtil.log("Search", "onItemSelected position: " + position + " selectedObject: " + arg0.getCount());
			textView.setText(arg0.getSelectedItem().toString());
			executeSearch(arg0.getSelectedItem().toString());
	  }

		public void onNothingSelected(AdapterView<?> arg0) {
	    // TODO Auto-generated method stub
	    
    }
	};

	public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
	  // TODO Auto-generated method stub
		LogUtil.log("Search", "onItemClick index: " + index);
		int selectedStoreId =  stores.get(index).mapGroupId ;
		
		LogUtil.log("Search", "onItemClick selectedStoreId: " + selectedStoreId);
		int realStoreId =  stores.get(index).storeId ;
		LogUtil.log("Search", "onItemClick realStoreId: " + realStoreId);
		// get the intent
    myIntent = new Intent(this, MapScreen.class);
    myIntent.putExtra(MapScreen.INTENT_KEY, AppIntent.SELECT_STORE);
    myIntent.putExtra(StoreService.SELECTED_STORE_KEY, selectedStoreId);
    myIntent.putExtra(StoreService.SELECTED_STORE_OVERRIDE, realStoreId);
    startActivityForResult(myIntent, 0);
	}
	
	
	
	
	

}
